package com.example.demo.controller;

import com.example.demo.dto.LoginRequest;
import com.example.demo.dto.LoginResponse;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * 提供用户相关的API接口
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    /**
     * 构造函数注入UserService
     * 
     * @param userService 用户服务
     */
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录接口
     * 
     * @param loginRequest 登录请求对象，包含用户名和密码
     * @return 登录响应对象，包含登录结果和消息
     */
    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
        // 参数校验
        if (loginRequest.getUsername() == null || loginRequest.getUsername().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(LoginResponse.failure("用户名不能为空"));
        }
        if (loginRequest.getPassword() == null || loginRequest.getPassword().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(LoginResponse.failure("密码不能为空"));
        }

        // 检查是否是超级管理员登录（用户名和密码都是admin）
        if ("admin".equals(loginRequest.getUsername()) && "admin".equals(loginRequest.getPassword())) {
            // 创建自定义的登录成功响应，包含特殊消息
            LoginResponse adminResponse = new LoginResponse(true, "登录成功，你是超级管理员，随便玩", loginRequest.getUsername());
            return ResponseEntity.ok(adminResponse);
        }
        
        // 调用服务层进行普通用户登录验证
        boolean loginSuccess = userService.login(loginRequest.getUsername(), loginRequest.getPassword());

        // 根据登录结果返回响应
        if (loginSuccess) {
            return ResponseEntity.ok(LoginResponse.success(loginRequest.getUsername()));
        } else {
            return ResponseEntity.ok(LoginResponse.failure("用户名或密码错误"));
        }
    }

    /**
     * 创建用户接口
     * 
     * @param loginRequest 包含用户名和密码的请求对象
     * @return 创建结果响应
     */
    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody LoginRequest loginRequest) {
        // 参数校验
        if (loginRequest.getUsername() == null || loginRequest.getUsername().trim().isEmpty()) {
            return ResponseEntity.badRequest().body("用户名不能为空");
        }
        if (loginRequest.getPassword() == null || loginRequest.getPassword().trim().isEmpty()) {
            return ResponseEntity.badRequest().body("密码不能为空");
        }

        try {
            // 创建用户对象并保存
            User user = new User(loginRequest.getUsername(), loginRequest.getPassword());
            userService.createUser(user);
            return ResponseEntity.ok("用户注册成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("注册失败：" + e.getMessage());
        }
    }
}